/*
 *  Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved.
 * 
 *       http://stripbandunk.com/
 * 
 *  STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.stripbandunk.tutorial.simpleapp.controller.impl;

import com.stripbandunk.tutorial.simpleapp.controller.GroupController;
import com.stripbandunk.tutorial.simpleapp.entity.Group;
import com.stripbandunk.tutorial.simpleapp.error.SimpleAppException;
import com.stripbandunk.tutorial.simpleapp.repository.GroupRepository;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Eko Kurniawan Khannedy
 */
@Controller
@Transactional(propagation = Propagation.REQUIRES_NEW,
rollbackFor = {SimpleAppException.class})
public class GroupControllerImpl implements GroupController {

    private GroupRepository repository;

    @Autowired(required = true)
    public void setRepository(GroupRepository repository) {
        this.repository = repository;
    }

    @Override
    public Group processCreate(Group group) {
        group.setCreatedDate(new Date());
        group.setLastModifiedDate(new Date());

        repository.save(group);

        return group;
    }

    @Override
    public void processDelete(Group... groups) {
        for (Group group : groups) {
            repository.delete(group);
        }
    }

    @Override
    public void processEdit(Group group) {
        group.setLastModifiedDate(new Date());

        repository.save(group);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Group> processLoad() {
        List<Group> groups = repository.findAll();
        return groups;
    }
}
